
package com.yzd.netmonitor.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.yzd.netmonitor.model.AsstClient;
import com.yzd.netmonitor.model.AsstMessage;
import com.yzd.netmonitor.service.AsstClientService;
import com.yzd.netmonitor.service.AsstMessageService;
import com.yzd.netmonitor.util.DESUtil;
import com.yzd.netmonitor.web.RespBody;
import com.yzd.netmonitor.web.ResponseCode;
import com.yzd.netmonitor.web.controller.param.ClientParams;

@RestController
@RequestMapping(value = "/client")
public class ClientController extends BaseController {

	@Autowired
	private AsstClientService asstClientServiceImpl;

	@Autowired
	private AsstMessageService asstMessageServiceImpl;

	public AsstClientService getAsstClientServiceImpl() {

		return asstClientServiceImpl;
	}

	public void setAsstClientServiceImpl(AsstClientService asstClientServiceImpl) {

		this.asstClientServiceImpl = asstClientServiceImpl;
	}

	public AsstMessageService getAsstMessageServiceImpl() {

		return asstMessageServiceImpl;
	}

	public void setAsstMessageServiceImpl(AsstMessageService asstMessageServiceImpl) {

		this.asstMessageServiceImpl = asstMessageServiceImpl;
	}

	// 注册
	@RequestMapping(value = "/register")
	public RespBody register(HttpServletRequest request) {

		// 获取请求数据
		Map<String, String[]> param = request.getParameterMap();
		String key = "12345678";
		// 解密
		String str = DESUtil.decode(key, param.toString());
		// 转换成json
		JSONObject jsonObject = (JSONObject) JSONObject.parse(str);
		// 获取参数
		String uuid = jsonObject.getString("uuid");
		String clientIp = jsonObject.getString("clientIp");
		String hostName = jsonObject.getString("hostName");
		// 判断参数合法性
		if (StringUtils.isBlank(uuid)) {
			return respWriter.toError(ResponseCode.CODE_301);
		}
		if (StringUtils.isBlank(clientIp)) {
			return respWriter.toError(ResponseCode.CODE_302);
		}
		if (StringUtils.isBlank(hostName)) {
			return respWriter.toError(ResponseCode.CODE_303);
		}
		AsstClient client = new AsstClient();
		client.setUuid(uuid);
		client.setClientIp(clientIp);
		client.setHostName(hostName);
		client.setRegisterTime(new Date().getTime());
		client.setLastRequestTime(new Date().getTime());
		asstClientServiceImpl.saveTest(client);
		logger.info("添加成功，id={}", client.getId());
		// 响应
		// 先试一下别纠结
		return respWriter.toSuccess(client.getId());
	}

	// 心跳
	@RequestMapping(value = "/hb")
	public RespBody hb(@RequestBody ClientParams params) {
		String clientIp = params.getClientIp();
		logger.info("clientIp:{}",clientIp);
		if (StringUtils.isBlank(clientIp)) {
			return respWriter.toError(ResponseCode.CODE_302);
		}
		// 响应
		List<AsstMessage> list = asstClientServiceImpl.getMessagesByClientIp(clientIp);
		// 所有Ip取完之后删除消息
		return respWriter.toSuccess(list);

	}

}
